/*
 * query_saletime_cb.c
 *
 *  Created on: 2013-2-5
 *      Author: panqiuhui
 */
#include <stdlib.h>
#include <string.h>
#include "../head/gui_callback/query_saletime_cb.h"
#include "../head/gui_callback/global_cb.h"
#include "../head/common_struct/traininfo.h"
#include "../head/communicate_with_server/traininfo_transfer.h"

extern GtkBuilder *builder;

//处理工具栏中的查询起售时间的点击事件
void toolbutton_query_saletime_clicked_cb(GtkToolButton *toolbutton,
		gpointer user_data) {
	GtkEntry *entry_query_saletime_station;
	GtkLabel *label_query_saletime_result;

	entry_query_saletime_station =
			GTK_ENTRY(gtk_builder_get_object(builder, "entry_query_saletime_station"));
	label_query_saletime_result =
			GTK_LABEL(gtk_builder_get_object(builder, "label_query_saletime_result"));

	gtk_entry_set_text(entry_query_saletime_station, "");
	gtk_label_set_label(label_query_saletime_result, "");

	window_show(QUERY_SALETIME);
}

//处理查询起售时间的查询按钮的点击事件
void button_query_saletime_clicked_cb(GtkButton *button, gpointer user_data) {
	GtkEntry *entry_query_saletime_station;
	GtkLabel *label_query_saletime_result;
	GtkWindow *global_window;

	global_window =
			GTK_WINDOW(gtk_builder_get_object(builder, "global_window"));
	entry_query_saletime_station =
			GTK_ENTRY(gtk_builder_get_object(builder, "entry_query_saletime_station"));
	label_query_saletime_result =
			GTK_LABEL(gtk_builder_get_object(builder, "label_query_saletime_result"));

	const gchar *station_name = gtk_entry_get_text(
			entry_query_saletime_station);
	int station_name_len = g_utf8_strlen(station_name,
			STATIONS_STATION_NAME_LEN);
	if (station_name_len == 0) {
		gtk_show_warning(global_window, "%s", "车站不能为空", "输入提醒");
		return;
	}

	struct stationinfo station;
	memset(&station, 0, sizeof(station));
	g_utf8_strncpy(station.stationName, station_name, station_name_len);

	bool query_result = query_saletime(&station);
	if (query_result) {
		guint hour;
		guint minute;
		guint second;
		sscanf(station.startSaleTime, "%02d:%02d:%02d", &hour, &minute,
				&second);
		char startsaletime[STATIONS_START_SALE_TIME_LEN];
		memset(startsaletime, 0, sizeof(startsaletime));
		sprintf(startsaletime, "%02d:%02d", hour, minute);
		gtk_label_set_label(label_query_saletime_result, startsaletime);
	} else {
		gtk_label_set_label(label_query_saletime_result, "未查询到结果");
	}
}
